package com.mch.main.common;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MLoader {

	private static List<ClassLoader> loaderList = new ArrayList<>();

	static {
		loaderList.add(Thread.currentThread().getContextClassLoader());
		loaderList.add(MLoader.class.getClassLoader());
		loaderList.add(ClassLoader.getSystemClassLoader());
	}

	public static InputStream getResourceAsStream(String resource) {
		for (ClassLoader cl : loaderList) {
			if (null != cl) {

				// try to find the resource as passed
				InputStream returnValue = cl.getResourceAsStream(resource);

				// now, some class loaders want this leading "/", so we'll add it and try again
				// if we didn't find the resource
				if (null == returnValue) {
					returnValue = cl.getResourceAsStream("/" + resource);
				}

				if (null != returnValue) {
					return returnValue;
				}
			}
		}
		return null;
	}
}
